package com.hahaliu.hahaliu.controller.user;

import com.hahaliu.hahaliu.common.result.Result;
import com.hahaliu.hahaliu.common.context.CurrentUser;
import com.hahaliu.hahaliu.pojo.entity.UserVideo;
import com.hahaliu.hahaliu.service.video.UserVideoService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("attitudeToVideoController")
@RequestMapping("/user/video")
@Api(tags = "用户端用户视频相关接口")
@Slf4j
public class UserVideoController {
	@Autowired
	private CurrentUser currentUser;

	@Autowired
	private UserVideoService userVideoService;


	/**
	 * 登录用户播放视频时更新播放次数，有30秒更新间隔（防止用户刷播放量）
	 * @param vid   视频ID
	 * @return  返回用户与该视频的交互数据
	 */

	//	TODO 因为刚开始的时候，视频数据是直接写入数据库的，到时候可能需要修改一下
	@PostMapping("/play")
	public Result<UserVideo> newPlayWithLoginUser(@RequestParam("vid") Integer vid) {
		Integer uid = currentUser.getUserId();
		UserVideo userVideo = userVideoService.updatePlay(uid, vid);

		return Result.success(userVideo);
	}

	/**
	 * 点赞或点踩
	 * @param vid   视频ID
	 * @param isLove    赞还是踩 true赞 false踩
	 * @param isSet     点还是取消 true点 false取消
	 * @return 返回用户与该视频更新后的交互数据
	 */
	@PostMapping("/love-or-not")
	public Result<UserVideo> loveOrNot(@RequestParam("vid") Integer vid,
	                                @RequestParam("isLove") boolean isLove,
	                                @RequestParam("isSet") boolean isSet) {
		Integer uid = currentUser.getUserId();
		UserVideo userVideo = userVideoService.setLoveOrUnlove(uid, vid, isLove, isSet);
		return Result.success(userVideo);
	}
}
